﻿

function appFactory(app) {
    //注入公用方法
    app.factory("method", function ($http, $q,$sce,$compile) {
        var ht = $http;
        var postCfg = {
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest: function (data) {
                return $.param(data);
            }
        };
        var method = {
            //后台请求数据
            htmlAjax: {
                //使用get方式获得数据 url 请求地址 data 传入数据格式为{name:"值"}
                get: function (url, data) {
                    var defer = $q.defer();
                    ht.get(url, { params: data }, postCfg).success(function (data) {
                        defer.resolve(data);
                    }).error(function (data) {
                        defer.resolve(data);
                    });
                    return defer.promise;
                },
                post: function (url, data) {
                    var defer = $q.defer();
                    ht.post(url, data, postCfg).success(function (data) {
                        defer.resolve(data);
                    }).error(function (data) {
                        defer.resolve(data);
                    });
                    return defer.promise;
                }
            },
            //拼接HTML连接地址
            getToUrl: function (url, type) {
                return url + "?type=" + type + "&sj=" + (new Date()).getTime();
            },
            //解析HTML
            htmltotext:function(text) {
                return $sce.trustAsHtml(text);
            },
            //获取当前url参数值
            getUrlPara: function (paraName) {
                var _cpath = window.location.href;
                var _css = _cpath.indexOf('?');
                var url = "";
                if (_css > -1) { url = _cpath.substring(_css + 1); }
                var _jw = url.lastIndexOf('#');
                if (_jw > -1) { url = url.substring(0, _jw); }

                var sUrl = "?" + url;
                if (sUrl.indexOf(paraName) > -1) {
                    var sReg = "(?:\\?|&){1}" + paraName + "=([^&]*)"
                    var re = new RegExp(sReg, "gi");
                    re.exec(sUrl);
                    return RegExp.$1;
                }
                return "";
            }
        }
        return method;
    });
}
